<template>
  <div class="app-container">
    <el-tabs v-model="activeName" type="card" class="elTabs">
      <el-tab-pane name="first">
        <div slot="label" class="pageHeader">字典管理</div>
      </el-tab-pane>
    </el-tabs>
    <div class="container">
      <div class="dictManage">
        <div class="searchBar" v-show="showSearch">
          <div class="searchForm">
            <el-form
              :model="queryParams"
              ref="queryForm"
              size="small"
              :inline="true"
            >
              <el-form-item label="字典名称:" prop="dictName">
                <el-input
                  class="input"
                  v-model.trim="queryParams.dictName"
                  placeholder="请输入字典名称"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="字典类型:" prop="dictType">
                <el-input
                  class="input"
                  v-model.trim="queryParams.dictType"
                  placeholder="请输入字典类型"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="状态:" prop="status">
                <el-select
                  class="input"
                  v-model="queryParams.status"
                  placeholder="请输入字典状态"
                  clearable
                >
                  <el-option
                    v-for="dict in dict.type.sys_normal_disable"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-form>
          </div>
          <div class="searchButtons">
            <el-button @click="handleQuery" class="searchButton"
              >搜索</el-button
            >
            <el-button @click="resetQuery" class="resetButton">重置</el-button>
          </div>
        </div>
        <div class="addDeleteBar">
          <el-button
            class="addButton"
            @click="handleAdd"
            v-hasPermi="['system:dict:add']"
            ><span class="plus">+</span>新增</el-button
          >
          <el-button
            icon="el-icon-delete"
            :class="!multiple ? 'deleteButton' : 'allDeleteButton'"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:dict:remove']"
            >批量删除</el-button
          >
          <el-button
            class="addButton"
            icon="el-icon-upload2"
            @click="handleExport"
            v-hasPermi="['system:dict:export']"
            >导出</el-button
          >
          <el-button
            class="deleteButton"
            icon="el-icon-refresh"
            @click="handleRefreshCache"
            v-hasPermi="['system:dict:remove']"
            >刷新缓存</el-button
          >
        </div>
        <!-- 表单选择清空 -->
        <div class="tableSelectArea">
          <div>
            <div class="iconInfo el-icon-info"></div>
            <span class="infoText">已选择</span>
            <span class="infoText infoActive" v-text="selectedNum"></span>
            <span class="infoText">项</span>
            <span class="infoButton" @click="clearSelect">清空</span>
          </div>
          <div>
            <right-toolbar
              :showSearch.sync="showSearch"
              @queryTable="getList"
            ></right-toolbar>
          </div>
        </div>
        <div class="table">
          <el-table
            ref="multipleTable"
            v-loading="loading"
            class="tableArea"
            :data="typeList"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column
              label="序号"
              width="100"
              align="center"
              type="index"
            />
            <el-table-column
              label="字典名称"
              align="center"
              width="150"
              prop="dictName"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="字典类型"
              align="center"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                <router-link
                  :to="'/system/dict-data/index/' + scope.row.dictId"
                  class="link-type"
                >
                  <span>{{ scope.row.dictType }}</span>
                </router-link>
              </template>
            </el-table-column>
            <el-table-column
              label="状态"
              align="center"
              prop="status"
              width="100"
            >
              <template slot-scope="scope">
                <dict-tag
                  :options="dict.type.sys_normal_disable"
                  :value="scope.row.status"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="备注"
              align="center"
              prop="remark"
              :show-overflow-tooltip="true"
            />
            <el-table-column label="操作" width="150" align="left">
              <template slot-scope="scope">
                <div class="tableButtonArea">
                  <el-button
                    type="text"
                    class="operationButton"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['system:dict:edit']"
                    >修改</el-button
                  >
                  <el-button
                    type="text"
                    class="operationButton"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['system:dict:remove']"
                    >删除</el-button
                  >
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div class="tableFooter">
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="open"
        class="dialogClass"
        width="500px"
        append-to-body
      >
        <template slot="title">
          <div class="modalTitle">
            <img
              src="../../../assets/bluelogo/drawerTitle.png"
              alt=""
              class="titleLogo"
            />{{ title }}
          </div>
        </template>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="字典名称" prop="dictName">
            <el-input v-model="form.dictName" placeholder="请输入字典名称" />
          </el-form-item>
          <el-form-item label="字典类型" prop="dictType">
            <el-input v-model="form.dictType" placeholder="请输入字典类型" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="dict.value"
                >{{ dict.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              :rows="5"
              v-model="form.remark"
              type="textarea"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="resetButton" @click="cancel">取 消</el-button>
          <el-button class="searchButton" type="primary" @click="submitForm"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
  refreshCache,
} from "@/api/system/dict/type";
import { PAGESIZE, PAGENUM, DICTEXPORTURL } from "../../../utils/variable";
export default {
  name: "Dict",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      activeName: "first",
      //列表选中数量
      selectedNum: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: PAGENUM,
        pageSize: PAGESIZE,
        dictName: "",
        dictType: "",
        status: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" },
        ],
        dictType: [
          { required: true, message: "字典类型不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //清空列表选择
    clearSelect() {
      this.$refs.multipleTable.clearSelection();
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      listType(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.typeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dictId: undefined,
        dictName: "",
        dictType: "",
        status: "0",
        remark: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = PAGENUM;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字典类型";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.dictId);
      this.selectedNum = selection.length;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dictId = row.dictId || this.ids;
      getType(dictId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改字典类型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.dictId !== undefined) {
            updateType(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addType(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictIds = row.dictId || this.ids;
      let num = this.selectedNum;
      if (row.dictId) {
        num = 1;
      }
      this.$modal
        .confirm("是否确认删除" + num + "条字典数据")
        .then(function () {
          return delType(dictIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        DICTEXPORTURL,
        {
          ...this.queryParams,
        },
        `type_${new Date().getTime()}.xlsx`
      );
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
        this.$store.dispatch("dict/cleanDict");
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../../assets/styles/variables.scss";
@import "../../../assets/styles/dialog.scss";
.app-container {
  height: 100%;
  padding: 0;
  .tableFooter {
    text-align: right;
    justify-content: flex-end;
    // margin-top: 25px;
    margin-right: 35px;
    // width: 100%;
    ::v-deep .btn-prev {
      color: #d4d3d3 !important;
      background-color: #fff;
      border: 1px solid #d4d3d3;
    }
    ::v-deep .btn-next {
      color: #d4d3d3 !important;
      background-color: #fff;
      border: 1px solid #d4d3d3;
    }
    ::v-deep .el-pager li {
      background-color: #fff;
      border: 1px solid #d4d3d3;
      font-size: 12px;
      font-family: $font-family;
      font-weight: 400;
      color: #5a5b5d;
    }
  }
}
.input {
  width: 150px;
}
.el-tabs--card > .el-tabs__header .el-tabs__nav {
  border: 0px;
}
::v-deep .el-tabs--card .el-tabs__header {
  background-color: #e4e7ed;
}
::v-deep.el-tabs--card .el-tabs__header .el-tabs__item {
  border-radius: 10px solid black;
  background-color: #fff;
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #053ef6;
  line-height: 48px;
  margin-top: 0;
}
.el-tabs--card {
  margin-left: 10px;
  border: 0px !important;
}
</style>
